package e3dds

import (
	"time"

	"gitee.com/ameise84/e3dds/internal/discov"
	"gitee.com/ameise84/e3dds/internal/rpc"
)

func DefaultServiceOptions() *ServiceOptions {
	return &ServiceOptions{
		mode:     BLModePoll,
		msgSize:  0, //消息队列长度(0:不限制长度)
		stayTime: 0, //最长待机时间(0:代表常驻)
		maxActor: 0, //actor最大数量(hash模式下如果不为0,则会将cid用于hash运算并绑定到固定对象上,其他模式如果为0,则采用系统默认数量)
	}
}

type ServiceOptions struct {
	mode     BLMode        //分发方式
	msgSize  int32         //消息队列长度
	stayTime time.Duration //最长驻留时间
	maxActor uint64        //最大actor数量.非hash模式使用
}

func (ts *ServiceOptions) SetMode(m BLMode) *ServiceOptions {
	ts.mode = m
	return ts
}

func (ts *ServiceOptions) SetMsgSize(size int32) *ServiceOptions {
	if size < 0 {
		panic("invalid msg size")
	}
	ts.msgSize = size
	return ts
}

func (ts *ServiceOptions) SetStayTime(t time.Duration) *ServiceOptions {
	ts.stayTime = t
	return ts
}

func (ts *ServiceOptions) SetMaxActorCnt(n uint64) *ServiceOptions {
	if n <= 0 {
		panic("invalid value for SetMaxActorCnt")
	}
	ts.maxActor = n
	return ts
}

func DefaultDcsOptions() *DcsOptions {
	return &DcsOptions{
		discoOptions: discov.DefaultOptions(),
		rpcOptions:   rpc.DefaultOptions(),
		callSlow:     2,
		callTimeOut:  30,
	}
}

type DcsOptions struct {
	discoOptions *discov.Options
	rpcOptions   *rpc.Options
	callSlow     int64
	callTimeOut  int64
}

func (ts *DcsOptions) GetDiscoOptions() *discov.Options {
	return ts.discoOptions
}

func (ts *DcsOptions) GetRPCOptions() *rpc.Options {
	return ts.rpcOptions
}

func (ts *DcsOptions) SetCallSlow(v int64) *DcsOptions {
	ts.callSlow = v
	return ts
}

func (ts *DcsOptions) SetCallTimeOut(v int64) *DcsOptions {
	ts.callTimeOut = v
	return ts
}
